/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package impresos;

import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.font.TextAttribute;
import java.awt.print.*;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.AttributedString;
import java.text.DecimalFormat;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;
import javax.swing.JTable;
import otros.Configuracion;
import otros.Util;
import tablasSql.ConsultaBD;

/**
 *
 * @author FERRETERIA
 */


class ObjetoDeImpVale extends Component implements Printable{
    
    private DatosToImprimir d;
    private int posY;
    private String fuente;
    
    public ObjetoDeImpVale (DatosToImprimir d) {
        super();
        this.d = d;
        this.fuente = Configuracion.getInstance().getProperty(Configuracion.FUENTE);
    } 
    
    
    private int getIniPosX() { 
       return 13;
    }
    
    private Image getImagen (String url) {
        URL imagenURL = null;
        try {
            imagenURL = new URL(url);
        } catch (MalformedURLException e) {
           e.printStackTrace();
        }
        return Toolkit.getDefaultToolkit().getImage(imagenURL);
    }
    
    public void EncabezadoA4(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
         
        MediaTracker mt = new MediaTracker(this);
         //imagen
        String ruta = "file:/" + Util.getRuta().replace("\\", "/") + Configuracion.getInstance().getProperty(Configuracion.LOGO_IMPT);
        if (ruta.contains("build"))
            ruta = "file:/D:/LogoFINALBN.jpg";
         
         Image imagen = getImagen(ruta);
         mt.addImage(imagen,0);
         try {
             mt.waitForID(0);
         } catch (InterruptedException e) {
         }
        
        int x = getIniPosX();
        int y = 13;
        posY = 75;
        g.drawImage(imagen, 10, 8, 205, 58,this);

        g.setFont(new Font (fuente, Font.BOLD, 9));
        g.drawString("FEMERCA S.L.", x, posY);      // x,y
        
        g.setFont(new Font (fuente, Font.PLAIN, 9));
        g.drawString("C/San Sebastian nº60", x, posY+=y);
        g.drawString("38005     S/C de Tenerife", x, posY+=y);
        g.drawString("Teléfono: 922 21 24 36", x, posY+=y);
        g.drawString("C.I.F:   B-38335535", x, posY+=y);
        g.drawString("Email: ferreteriamercado@hotmail.com", x,posY+=y);

        g.drawLine(x, posY+=5, 210, posY);
    }
     
    public void Datos(Graphics g) {
        int x = getIniPosX();
        int y = 13;
        
        g.setFont(new Font (fuente, Font.BOLD, 12));
        g.drawString("VALE DE CAJA", x+45, posY+=y);
        g.setFont(new Font (fuente, Font.BOLD, 10));
        g.drawString(d.getTipoTicket()+": "+d.getNumTicket(), x+15, posY+=y);
        
        g.setFont(new Font (fuente, Font.PLAIN, 9));
        g.drawString("Fecha: ", x+15, posY+=y);
        g.setFont(new Font (fuente, Font.BOLD, 9));
        g.drawString(d.getFecha(), x+45, posY);
                
        g.setFont(new Font (fuente, Font.BOLD, 12));
        g.drawString(" TOTAL " + d.getPrecioMano(), 116, posY+=20);
        
    }
    
    public void Pie(Graphics g) {
         
    }
               
    public int print(Graphics g, PageFormat f, int pageIndex){
        switch (pageIndex) {
            case 0:
               EncabezadoA4(g);
               Datos(g);
        //       Pie(g);
               return PAGE_EXISTS;
            default:
                return NO_SUCH_PAGE;
        }
    }
}

public class ImpVale {
    
    static String impresora;
    
    void Imprimir () {
        setImpresora();
    }
    
    static void setImpresora () {
        impresora = Configuracion.getInstance().getProperty(Configuracion.IMP_TICKET);
    }
    
    static public void imprime(DatosToImprimir d){
        ConsultaBD.cursorEspera(null);
        setImpresora();
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        Paper paper = new Paper();
        double margin;
        paper.setSize(30*72, 500);
        margin = 0.5;
        
        paper.setImageableArea(margin, margin, paper.getWidth()-margin, paper.getHeight()-margin);
        pf.setPaper(paper);
        pf.setOrientation(PageFormat.PORTRAIT);  // vertical
        job.setJobName("vale");
        
        //Busco impresora
        AttributeSet attributeSet = new HashAttributeSet();
        attributeSet.add(new PrinterName(impresora, null));
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet);
        
        try{
           job.setPrintService(services[0]);
           job.setPrintable(new ObjetoDeImpVale(d), pf);  
           job.print();
           
        } catch(PrinterException e){
             System.out.println(e);
        }

        ConsultaBD.cursorNormal(null);
    }
}
